package com.example.mycarcalling.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.lib_cache.service.ApiService;
import com.example.lib_cache.viewmodel.PasswordEditActivityViewModel;
import com.example.lib_cache.viewmodel.RegisterViewModel;
import com.example.mycarcalling.R;
import com.example.mycarcalling.databinding.ActivityRegisterBinding;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class RegisterActivity extends AppCompatActivity {

    private final static String TAG = RegisterActivity.class.getSimpleName();
    private boolean pwdSwitch = false;


    @Inject
    ApiService apiService;

    private Context context = null;
    private RegisterViewModel registerViewModel = null;
    private ActivityRegisterBinding activityRegisterBinding = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityRegisterBinding = DataBindingUtil.setContentView(
                this,R.layout.activity_register);
        context =this;

        initData();
        initView();
        initListener();
        initObserve();

    }

    private void initData(){
        //创建数据源
        //创建一个ViewModel的工厂
        ViewModelProvider.Factory factory = ViewModelProvider.
                AndroidViewModelFactory.getInstance(getApplication());
        //创建ViewModel的管理对象
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        //生产ViewModel
        registerViewModel = viewModelProvider.get(RegisterViewModel.class);
    }

    private void initView(){
        setTitle("注册");
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(100);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);

        Glide.with(context)
                .load(R.mipmap.register)
                .apply(options)
                .into(activityRegisterBinding.ivRegister);
    }

    private void initListener(){
        activityRegisterBinding.tietAccountRegister.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = activityRegisterBinding.tietAccountRegister.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityRegisterBinding.tietAccountRegister.setError("请输入账号");
                    registerViewModel.setAccount("");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        registerViewModel.setAccount(str);
                    }
                    else {
                        activityRegisterBinding.tietAccountRegister.setError("请输入6-20位数的英文字母与数字的组合");
                        registerViewModel.setAccount("");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = activityRegisterBinding.tietAccountRegister.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityRegisterBinding.tietAccountRegister.setError("请输入账号");
                    registerViewModel.setAccount("");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        registerViewModel.setAccount(str);
                    }
                    else {
                        activityRegisterBinding.tietAccountRegister.setError("请输入6-20位数的英文字母与数字的组合");
                        registerViewModel.setAccount("");
                    }
                }
            }
        });

        activityRegisterBinding.tietPasswordRegister.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = activityRegisterBinding.tietPasswordRegister.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityRegisterBinding.tietPasswordRegister.setError("请输入密码");
                    registerViewModel.setPassword("");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        if(str.equals(
                                activityRegisterBinding.tietPasswordConfirmRegister.getText().toString())){
                            registerViewModel.setPassword(str);
                        }else {
                            activityRegisterBinding.tietPasswordRegister.setError("两次新密码不一致");
                            registerViewModel.setPassword("");
                        }
                    }
                    else {
                        activityRegisterBinding.tietPasswordRegister.setError("请输入6-20位数的英文字母与数字的组合");
                        registerViewModel.setPassword("");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = activityRegisterBinding.tietPasswordRegister.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityRegisterBinding.tietPasswordRegister.setError("请输入密码");
                    registerViewModel.setPassword("");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        if(str.equals(
                                activityRegisterBinding.tietPasswordConfirmRegister.getText().toString())){
                            registerViewModel.setPassword(str);
                        }else {
                            activityRegisterBinding.tietPasswordRegister.setError("两次新密码不一致");
                            registerViewModel.setPassword("");
                        }
                    }
                    else {
                        activityRegisterBinding.tietPasswordRegister.setError("请输入6-20位数的英文字母与数字的组合");
                        registerViewModel.setPassword("");
                    }
                }
            }
        });

        activityRegisterBinding.tietPasswordConfirmRegister.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = activityRegisterBinding.tietPasswordConfirmRegister.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityRegisterBinding.tietPasswordConfirmRegister.setError("请输入密码");
                    registerViewModel.setPassword("");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        if(str.equals(
                                activityRegisterBinding.tietPasswordRegister.getText().toString())){
                            registerViewModel.setPassword(str);

                        }else {
                            activityRegisterBinding.tietPasswordConfirmRegister.setError("两次新密码不一致");
                            registerViewModel.setPassword("");
                        }
                    }
                    else {
                        activityRegisterBinding.tietPasswordConfirmRegister.setError("请输入6-20位数的英文字母与数字的组合");
                        registerViewModel.setPassword("");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = activityRegisterBinding.tietPasswordConfirmRegister.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityRegisterBinding.tietPasswordConfirmRegister.setError("请输入密码");
                    registerViewModel.setPassword("");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        if(str.equals(
                                activityRegisterBinding.tietPasswordRegister.getText().toString())){
                            registerViewModel.setPassword(str);

                        }else {
                            activityRegisterBinding.tietPasswordConfirmRegister.setError("两次新密码不一致");
                            registerViewModel.setPassword("");
                        }
                    }
                    else {
                        activityRegisterBinding.tietPasswordConfirmRegister.setError("请输入6-20位数的英文字母与数字的组合");
                        registerViewModel.setPassword("");
                    }
                }
            }
        });

        activityRegisterBinding.ivShowPwdRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pwdSwitch = !pwdSwitch;
                if (pwdSwitch){
                    activityRegisterBinding.ivShowPwdRegister.setImageResource(
                            R.drawable.ic_baseline_remove_red_eye_24);
                    activityRegisterBinding.tietPasswordRegister.setInputType(
                            InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    activityRegisterBinding.tietPasswordConfirmRegister.setInputType(
                            InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {

                    activityRegisterBinding.ivShowPwdRegister.setImageResource(
                            R.drawable.ic_baseline_visibility_off_23);

                    activityRegisterBinding.tietPasswordRegister.setInputType(
                            InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                    activityRegisterBinding.tietPasswordRegister.setTypeface(Typeface.DEFAULT);
                    activityRegisterBinding.tietPasswordConfirmRegister.setInputType(
                            InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                    activityRegisterBinding.tietPasswordConfirmRegister.setTypeface(Typeface.DEFAULT);
                }
            }
        });

        activityRegisterBinding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(registerViewModel.editCheck()){
                    registerViewModel.requestRegister(apiService,context);
                }else {
                    Toast.makeText(context, "输入不规范，请检查输入", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initObserve(){
        registerViewModel.getRegisterAccount().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
                setResult(1);
                finish();
            }
        });
    }
}